package com.taobao.home.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.taobao.base.model.PageParams;
import com.taobao.base.model.PageResult;
import com.taobao.base.model.RestResponse;
import com.taobao.base.utils.StringUtil;
import com.taobao.home.mapper.GoodsMapper;
import com.taobao.home.mapper.StoreMapper;
import com.taobao.home.model.dto.StoreDto;
import com.taobao.home.model.dto.StorePreviewDto;
import com.taobao.home.model.po.Store;
import com.taobao.home.model.po.StorePreview;
import com.taobao.home.service.StoreService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class StoreServiceImpl implements StoreService {

    @Autowired
    private StoreMapper storeMapper;
    @Autowired
    private GoodsMapper goodsMapper;


    @Override
    public RestResponse<List<StoreDto>> getAllStore() {
        LambdaQueryWrapper<Store> queryWrapper = new LambdaQueryWrapper<>();

        List<StoreDto> storeDtoList = new ArrayList<>();
        List<Store> storeList = storeMapper.selectList(queryWrapper);

        for (Store store : storeList) {
            StoreDto storeDto = new StoreDto();
            BeanUtils.copyProperties(store, storeDto);
            storeDtoList.add(storeDto);
        }

        return RestResponse.success(storeDtoList);
    }

    @Override
    public PageResult<StorePreviewDto> getStorePreview(PageParams pageParams) {

        List<StorePreview> previewList = storeMapper.queryPreview((long) pageParams.getPageNo(), (long) pageParams.getPageSize());

        if (previewList == null) {
            return null;
        }

        List<StorePreviewDto> previewDtoList = new ArrayList<>();

        for (StorePreview preview : previewList) {
            StorePreviewDto previewDto = new StorePreviewDto();
            BeanUtils.copyProperties(preview, previewDto, "imgList");
            previewDto.setImgList(StringUtil.StringToList(preview.getImgList()));
            previewDtoList.add(previewDto);
        }


        return new PageResult(previewDtoList, previewList.size(), pageParams.getPageNo(), pageParams.getPageSize());
    }

    @Override
    public RestResponse<StoreDto> getStoreById(Integer storeId) {
        Store store = storeMapper.queryStoreById(storeId);
        if (store == null) {
            System.err.println("getStoreById   店铺不存在");
            return RestResponse.validfail("店铺不存在");
        }
        StoreDto storeDto = new StoreDto();
        BeanUtils.copyProperties(store, storeDto);

        return RestResponse.success(storeDto);
    }

    @Override
    public RestResponse<String> addStore(StoreDto storeDto, int userId) {


        Store store = new Store();
        BeanUtils.copyProperties(storeDto, store);
        store.setEmail("****");
        store.setWebsite("****");
        store.setStoreIcon("https://gw.alicdn.com/tfs//i4/1114511827/O1CN019Zx9wo1PMo5lyfVvo_!!1114511827.jpg");
        store.setBusinessHours("周一至周日 9:00-22:00");
        store.setOwnerId(userId);
        store.setStatus(1);
        store.setCreateTime(LocalDateTime.now());
        store.setLatitude(0.00);
        store.setLongitude(0.00);
        store.setRemark("暂无");
        store.setReleaseTime(LocalDateTime.now());
        store.setLabel("暂无");
        store.setTitle("暂无");
        store.setImgList("暂无");
        store.setBuyNum(0);
        store.setLookNum(0);
        int i = storeMapper.insert(store);
        if (i == 0) {
            return RestResponse.validfail("添加失败");
        }
        return RestResponse.success("添加成功");
    }

    @Override
    @Transactional
    public RestResponse<String> deleteStore(int storeId) {

        // 1. 删除店铺下的商品
        this.goodsMapper.deleteGoodsByStoreId(storeId);

        // 2. 删除店铺
        int i = this.storeMapper.deleteStoreById(storeId);
        if (i == 0) {
            return RestResponse.validfail("删除失败");
        }
        return RestResponse.success("删除成功");
    }

    @Override
    public RestResponse<List<StoreDto>> getMyStore(int userId) {

        List<Store> stores = storeMapper.queryMyStore(userId);

        List<StoreDto> storeDtoList = new ArrayList<>();
        for (Store store : stores) {
            StoreDto storeDto = new StoreDto();
            BeanUtils.copyProperties(store, storeDto);
            storeDtoList.add(storeDto);
        }
        return RestResponse.success(storeDtoList);

    }
}
